﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NexCart.Web.Models;

namespace NexCart.Web.Repositories {
	public class SqlProductRepository : NexCart.Web.Repositories.IProductRepository {

		NexCartDataContext db;

		public SqlProductRepository(NexCartDataContext db) {
			this.db = db;
		}

		public IQueryable<Product> Products {
			get {
				return db.Products;
			}
		}

		public Product Save(Product product) {
			if (product.Id == 0)
				db.Products.InsertOnSubmit(product);

			db.SubmitChanges();
			return product;
		}

		public Product Single(int id) {
			return db.Products.SingleOrDefault(p => p.Id == id);
		}

		public void Delete(int id) {
			db.Products.DeleteOnSubmit(Single(id));
			db.SubmitChanges();
		}
	}
}
